<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="任职开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginDate">
              <!-- <j-date :disabled=formDisabled placeholder="请选择任职开始日期" v-model="model.beginDate" style="width: 100%"/> -->
              <a-date-picker :disabled="formDisabled" placeholder="请选择任职开始日期" valueFormat="YYYY-MM-DD" @change="handleChange" v-model="model.beginDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="任职结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <!-- <j-date :disabled=formDisabled placeholder="请选择任职结束日期" v-model="model.endDate" style="width: 100%"/> -->
              <a-date-picker
                :disabled="formDisabled"
                :disabled-date="disabledDate"
                :default-picker-value="this.model.beginDate"
                placeholder="请选择任职结束日期"
                valueFormat="YYYY-MM-DD"
                v-model="model.endDate"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="职务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionName">
<!--              <j-select-position :disabled=formDisabled v-model="model.positionName" type="list" placeholder="请输入职务名称"/>-->
              <!-- <j-search-select-tag :disabled=formDisabled v-model="model.positionName" placeholder="请输入职务名称"  dict="erp_org_position_level,level_name,id"/> -->
              <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.positionName"
                                     dictCode="erp_org_position_level,level_name,id" placeholder="请选择职务名称"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="任职文号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="docomentNo">
              <a-input :disabled=formDisabled v-model="model.docomentNo" placeholder="请输入任职文号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="批准单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalUnit">
              <j-select-depart :disabled=formDisabled v-model="model.approvalUnit" placeholder="请输入批准单位" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { validateBlankValue } from '@/utils/util'
import moment from 'moment';

export default {
  name: 'ErpEmployeePositionChangeRecordForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        positionName: [
          { required: true, message: '请输入职务名称!' },
          { validator: (rule, value, callback) => validateBlankValue('erp_employee_position_change_record', 'position_name', value, this.model.id, callback) },
        ],
        approvalUnit: [
          { required: true, message: '请输入批准单位!' },
          { validator: (rule, value, callback) => validateBlankValue('erp_employee_position_change_record', 'approval_unit', value, this.model.id, callback) },
        ],
      },
      url: {
        add: '/api/erp/files/employee/erpEmployeePositionChangeRecord/add',
        edit: '/api/erp/files/employee/erpEmployeePositionChangeRecord/edit',
        queryById: '/api/erp/files/employee/erpEmployeePositionChangeRecord/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    moment,
    handleChange(value) {
      if (this.model.endDate) {
        if ((this.model.endDate).diff((this.model.beginDate), 'days') < 0) {
          this.model.endDate = this.model.beginDate
        }
      }
    },
    disabledDate(current) {
      return current < moment(this.model.beginDate).subtract('day')
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.$refs.form.clearValidate()
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          // that.confirmLoading = true;
          // let httpurl = '';
          // let method = '';
          // if (!this.model.id) {
          //   httpurl += this.url.add;
          //   method = 'post';
          // } else {
          //   httpurl += this.url.edit;
          //   method = 'put';
          // }
          // httpAction(httpurl, this.model, method).then((res) => {
          //   if (res.success) {
          //     that.$message.success(res.message);
          //     that.$emit('ok');
          //   } else {
          //     that.$message.warning(res.message);
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false;
          // })
          that.$emit('ok',that.model);
        }

      })
    },
  }
}
</script>
